// pages/prePaid/prePaid.js
var anmt;

var app = getApp();

var common = require("../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        toastHide: true,
        current: 0,
        current_scroll: "tab1",
        balanceModule: true,
        bgHidden: true,
        flag: true,
        currentChoose: 0
    },
    choosePrice(e) {
        this.setData({
            currentChoose: e.currentTarget.dataset.index,
            currentPrepaid: e.currentTarget.dataset.price
        });
    },
    getBlanceSet() {
        let that = this;
        common.request({
            endpoint: "Balance/balance/balance-set-read",
            method: "post",
            data: {},
            success: res => {
                if (res.code == 0) {
                    if (res.extra.switch != 1) {
                        that.setData({
                            flag: false
                        });
                    }
                }
            }
        });
    },
    //余额提现界面
    goToBalanceWithdrawal() {
        wx.navigateTo({
            url: "/blanceWithdrawal/pages/blanceWithdrawal/blanceWithdrawal?blanceNum=" + this.data.balanceNum
        });
    },
    // tab切换
    tabChangeScroll({detail: detail}) {
        this.setData({
            current: detail.key,
            currentPrepaid: this.data.prpaidList[detail.key].condition_price
        });
    },
    //获取方案列表
    getPrePaid() {
        var that = this;
        common.request({
            endpoint: "Balance/balance/read_basic",
            method: "post",
            data: {},
            success(res) {
                var prpaidList = res.extra.recharge_info;
                prpaidList.forEach((item, index) => {
                    //方案列表，添加数据下标
                    item.index = index;
                    var giveArry = [];
                    if (item.give_price > 0) {
                        giveArry.push(item.give_price + "元");
                    }
                    if (item.voucher) {
                        giveArry.push(item.voucher);
                    }
                    if (item.give_integral > 0) {
                        giveArry.push(item.give_integral + "积分");
                    }
                    if (giveArry.length > 0) {
                        item.give = giveArry.join("+");
                    } else {
                        item.give = "无赠送";
                    }
                    if (index > 4) {
                        item.num = Math.floor(Math.random() * (5 - 0)) + 0;
                    } else {
                        item.num = index;
                    }
                });
                that.setData({
                    current: 0,
                    balanceNum: res.extra.balance,
                    //余额金额
                    prpaidList: prpaidList,
                    //充值方案
                    currentPrepaid: res.extra.recharge_info.length > 0 ? res.extra.recharge_info[0].condition_price : 0
                });
            }
        });
    },
    //充值按钮
    prepaidButt() {
        var that = this;
        //判断充值金额有没有开通
                if (that.data.features.prepaid == 1) {
            //充值功能已开通
            //判断充值金额
            if (that.data.currentPrepaid > 0) {
                //充值金额大于0
                //判断赠送条件有没有变化
                that.checkCondition();
            } else {
                //充值金额<=0
                that.toastControl("充值金额必须大于0");
            }
        } else {
            //充值功能未开通,提示功能未开通
            that.toastControl("充值功能未开通");
        }
    },
    //判断赠送条件有没有变化
    checkCondition() {
        var that = this;
        var item = that.data.prpaidList[that.data.currentChoose];
        //当前充值方案
                that.setData({
            buttDisabled: true
        });
        common.request({
            endpoint: "Balance/balance/check_condition",
            method: "post",
            data: {
                set_id: item.set_id,
                other: {
                    give_price: item.give_price,
                    give_integral: item.give_integral,
                    voucher_id: item.voucher_id,
                    price: item.condition_price
                }
            },
            success(res) {
                if (res.message == "success") {
                    //充值条件正常
                    that.rechargePay();
                } else {}
            },
            fail(err) {
                if (err.code == -1) {
                    wx.showModal({
                        title: "充值条件发生改变",
                        content: "是否获取最新充值方案",
                        success(res) {
                            //确定，重新获取充值方案
                            if (res.confirm) {
                                that.setData({
                                    buttDisabled: false
                                });
                                that.getPrePaid();
                            } else if (res.cancel) {
                                that.setData({
                                    buttDisabled: false
                                });
                            }
                        }
                    });
                }
            }
        });
    },
    //余额充值
    rechargePay() {
        var that = this;
        var item = that.data.prpaidList[that.data.currentChoose];
        //当前充值方案
                common.request({
            endpoint: "Balance/balance/recharge-pay",
            method: "post",
            data: {
                set_id: item.set_id
            },
            success(res) {
                var detail_id = res.extra.detail_id;
                wx.requestPayment({
                    timeStamp: res.extra.pay_info.timeStamp,
                    nonceStr: res.extra.pay_info.nonceStr,
                    package: res.extra.pay_info.package,
                    signType: res.extra.pay_info.signType,
                    paySign: res.extra.pay_info.paySign,
                    success: function(res) {
                        //余额充值下单成功，调用微信支付
                        wx.showToast({
                            title: "支付成功",
                            icon: "success",
                            mask: true,
                            duration: 3e3,
                            success: function(res) {
                                wx.showToast({
                                    title: "支付成功！",
                                    icon: "success"
                                });
                                //改变订单状态
                                                                common.request({
                                    endpoint: "Balance/balance/sync-notify",
                                    method: "post",
                                    data: {
                                        detail_id: detail_id
                                    },
                                    success(res) {
                                        that.getPrePaid();
                                        that.setData({
                                            buttDisabled: false
                                        });
                                    }
                                });
                            }
                        });
                    },
                    fail: function(res) {
                        that.setData({
                            buttDisabled: false
                        });
                    }
                });
            }
        });
    },
    //跳转明细
    goToBalanceDetail() {
        wx.navigateTo({
            url: "/pages/balanceDetail/balanceDetail"
        });
    },
    //轻提示框
    toastControl(content, time) {
        var that = this;
        that.setData({
            toastContent: content,
            toastHide: false
        });
        //显示提示框
                setTimeout(() => {
            that.setData({
                toastHide: true
            });
        }, !time ? 3e3 : time);
        //定时隐藏提示框,默认3秒后隐藏
        },
    //获取基础配置
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarTitle({
                    title: res.extra.title
                });
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style,
                    leader_change: res.extra.l_change,
                    leader_express: res.extra.l_express,
                    rp_name: res.extra.rp_name,
                    order_start: res.extra.order_start,
                    order_start_switch: res.extra.order_start_switch
                });
                //处理模块开关
                                var features = common.getModulesSwich(res.extra.features);
                //处理数据格式
                                that.setData({
                    features: features
                });
            }
        });
    },
    getBalanceSwitch: function() {
        var that = this;
        common.request({
            endpoint: "Balance/balance/balance_switch",
            method: "post",
            data: {},
            success(res) {
                that.setData({
                    balanceSwitch: res.extra
                });
            }
        });
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        wx.hideShareMenu();
        var that = this;
        that.getBase();
        //获取全局配置
        //anmt=wx.createAnimation()
                that.getPrePaid();
        //获取充值方案
                that.getBlanceSet();
        that.getBalanceSwitch();
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {}
});